package cn.iocoder.yudao.module.system.service.worksaleinventory;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils;
import cn.iocoder.yudao.module.system.dal.dataobject.workorder.WorkOrderDO;
import cn.iocoder.yudao.module.system.dal.mysql.workorder.WorkOrderMapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;
import cn.iocoder.yudao.module.system.controller.admin.worksaleinventory.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.worksaleinventory.WorkSaleInventoryDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.system.dal.mysql.worksaleinventory.WorkSaleInventoryMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;

/**
 * 销售入库 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class WorkSaleInventoryServiceImpl implements WorkSaleInventoryService {

    @Resource
    private WorkOrderMapper workOrderMapper;
    @Resource
    private WorkSaleInventoryMapper workSaleInventoryMapper;

    @Override
    public Integer createWorkSaleInventory(WorkSaleInventorySaveReqVO createReqVO) {
        WorkOrderDO workOrderData = workOrderMapper.selectById(createReqVO.getWorkId());
        // 插入
        WorkSaleInventoryDO workSaleInventory = BeanUtils.toBean(createReqVO, WorkSaleInventoryDO.class);
        workSaleInventory.setCustomerId(workOrderData.getCustomerId());
        workSaleInventoryMapper.insert(workSaleInventory);

        Long userId = WebFrameworkUtils.getLoginUserId();
        //更新订单信息
        UpdateWrapper<WorkOrderDO> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",createReqVO.getWorkId());
        if(workOrderData.getStorageMethod().equals("1")){
            if(createReqVO.getQuantity()>0){
                updateWrapper.setSql("finished_product_num = finished_product_num + "+createReqVO.getQuantity());
            }else{
                updateWrapper.setSql("cp_defect_num = cp_defect_num + "+createReqVO.getQuantity());
            }
        }else{
            if(createReqVO.getWeight().compareTo(BigDecimal.valueOf(0))>0) {
                updateWrapper.setSql("finished_product_num = finished_product_num + "+createReqVO.getWeight());
            }else{
                updateWrapper.setSql("cp_defect_num = cp_defect_num + "+createReqVO.getWeight());
            }
        }
        workOrderMapper.update(updateWrapper);
        // 返回
        return workSaleInventory.getId();
    }

    @Override
    public void updateWorkSaleInventory(WorkSaleInventorySaveReqVO updateReqVO) {
        // 校验存在
        validateWorkSaleInventoryExists(updateReqVO.getId());
        // 更新
        WorkSaleInventoryDO updateObj = BeanUtils.toBean(updateReqVO, WorkSaleInventoryDO.class);
        workSaleInventoryMapper.updateById(updateObj);
    }

    @Override
    public void deleteWorkSaleInventory(Integer id) {
        // 校验存在
        validateWorkSaleInventoryExists(id);
        // 删除
        workSaleInventoryMapper.deleteById(id);
    }

    @Override
        public void deleteWorkSaleInventoryListByIds(List<Integer> ids) {
        // 校验存在
        validateWorkSaleInventoryExists(ids);
        // 删除
        workSaleInventoryMapper.deleteByIds(ids);
        }

    private void validateWorkSaleInventoryExists(List<Integer> ids) {
        List<WorkSaleInventoryDO> list = workSaleInventoryMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(WORK_SALE_INVENTORY_NOT_EXISTS);
        }
    }

    private void validateWorkSaleInventoryExists(Integer id) {
        if (workSaleInventoryMapper.selectById(id) == null) {
            throw exception(WORK_SALE_INVENTORY_NOT_EXISTS);
        }
    }

    @Override
    public WorkSaleInventoryDO getWorkSaleInventory(Integer id) {
        return workSaleInventoryMapper.selectById(id);
    }

    @Override
    public PageResult<WorkSaleInventoryDO> getWorkSaleInventoryPage(WorkSaleInventoryPageReqVO pageReqVO) {
        return workSaleInventoryMapper.selectPage(pageReqVO);
    }

}
